package com.gopay.bis.ccb.ebank.operation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.exception.GopayException;

/**
 * 将交易信息发送到银行
 * 
 * @SocketTransportImpl.java
 * @author WangHongFei
 */
public class B2bSocketTransportImpl {

	private Map<String, String> proMap;

	public Object submit(Object obj) throws GopayException{
		BaseMessage baseMessage = new BaseMessage();
		baseMessage.setOutStlmCd(BankConstants.BANK_LOGO_CCB); // 为了区分日志输出
		
		BufferedWriter bw = null;
		OutputStreamWriter ow = null;
		StringBuffer stringBuffer = new StringBuffer();
		Socket socketClient = null;
		try {
			//与银行换取连接socket
			String host = proMap.get("host");
			int port = Integer.valueOf(proMap.get("port"));
			int timeout = Integer.valueOf(proMap.get("timeout"));
			int receiveBufferSize = Integer.valueOf(proMap.get("receiveBufferSize"));
			int sendBufferSize = Integer.valueOf(proMap.get("sendBufferSize"));
			socketClient = new Socket(host, port);
			socketClient.setSoTimeout(timeout);
			socketClient.setReceiveBufferSize(receiveBufferSize);
			socketClient.setSendBufferSize(sendBufferSize);			
			if (socketClient != null) {
				// 提交报文
				ow = new OutputStreamWriter(socketClient.getOutputStream());
				bw = new BufferedWriter(ow);
				String reqData = (String) obj;
				BankLogUtils.printB2BQuerySendLog(baseMessage, reqData);
				bw.write(reqData);
				bw.flush();
				// 接收返回内容,根据文档直接是个字符串
				BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
				String returnContent = "";
	            while ((returnContent = reader.readLine()) != null) {
	                stringBuffer.append(returnContent);
	            }
				BankLogUtils.printB2BQueryBlackLog(baseMessage, stringBuffer.toString());
			}
			return stringBuffer.toString();
		} catch (Exception e) {
			BankLogUtils.printB2BQueryExceptionLog(baseMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					BankLogUtils.printB2BQueryExceptionLog(baseMessage, e);
					throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
				}
			}
			if (ow != null) {
				try {
					ow.close();
				} catch (IOException e) {
					BankLogUtils.printB2BQueryExceptionLog(baseMessage, e);
					throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
				}
			}
			if (socketClient != null) {
				try {
					socketClient.close();
				} catch (IOException e) {
					BankLogUtils.printB2BQueryExceptionLog(baseMessage, e);
					throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
				}
			}
		}
	}

	public void setProMap(Map<String, String> proMap) {
		this.proMap = proMap;
	}

}
